package org.go.scheduler.database.concurrent;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.go.expcetion.LockException;
import org.go.util.Util;

/**
 * Internal database based lock handler for providing thread/resource locking 
 * in order to protect resources from being altered by multiple threads at the 
 * same time.
 * 
 * @author jhouse
 */
public class StdRowLockSemaphore extends DBSemaphore {

	public static final String INSERT_LOCK = "INSERT INTO " + TABLE_PREFIX_SUBST + TABLE_LOCKS + "(" + COL_SCHEDULER_NAME + ", " + COL_LOCK_NAME + ") VALUES (" + SCHED_NAME_SUBST + ", ?)";
	public static final String SELECT_FOR_LOCK = "SELECT * FROM " + TABLE_PREFIX_SUBST + TABLE_LOCKS + " WHERE " + COL_SCHEDULER_NAME + " = " + SCHED_NAME_SUBST + " AND " + COL_LOCK_NAME + " = ? FOR UPDATE";

	public StdRowLockSemaphore(String tablePrefix, String schedName, String selectWithLockSQL) {
		super(tablePrefix, schedName, selectWithLockSQL != null ? selectWithLockSQL : SELECT_FOR_LOCK, INSERT_LOCK);
	}

	/**
	 * Execute the SQL select for update that will lock the proper database row.
	 */
	@Override
	protected void executeSQL(Connection conn, String lockName, String expandedSQL, String expandedInsertSQL) throws LockException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			getLog().error("executeSQL:{}", expandedSQL);
			getLog().error("expandedInsertSQL:{}", expandedInsertSQL);
			ps = conn.prepareStatement(expandedSQL);
			ps.setString(1, lockName);

			if (getLog().isDebugEnabled()) {
				getLog().debug("Lock '" + lockName + "' is being obtained: " + Thread.currentThread().getName());
			}
			rs = ps.executeQuery();
			if (!rs.next()) {
				getLog().debug("Inserting new lock row for lock: '" + lockName + "' being obtained by thread: " + Thread.currentThread().getName());
				ps = conn.prepareStatement(expandedInsertSQL);
				ps.setString(1, lockName);

				int res = ps.executeUpdate();

				if (res != 1)
					throw new SQLException(Util.rtp("No row exists, and one could not be inserted in table " + TABLE_PREFIX_SUBST + TABLE_LOCKS + " for lock named: " + lockName, getTablePrefix(), getSchedulerNameLiteral()));

			}
		} catch (SQLException sqle) {
			//Exception src =
			// (Exception)getThreadLocksObtainer().get(lockName);
			//if(src != null)
			//  src.printStackTrace();
			//else
			//  System.err.println("--- ***************** NO OBTAINER!");

			if (getLog().isDebugEnabled()) {
				getLog().debug("Lock '" + lockName + "' was not obtained by: " + Thread.currentThread().getName());
			}

			throw new LockException("Failure obtaining db row lock: " + sqle.getMessage(), sqle);
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (Exception ignore) {
				}
			}
		}
	}

	protected String getSelectWithLockSQL() {
		return getSQL();
	}

	public void setSelectWithLockSQL(String selectWithLockSQL) {
		setSQL(selectWithLockSQL);
	}
}
